﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace JsonCodeGenerator
{
    /// <summary>
    /// 属性注释添加
    /// </summary>
    internal class XmlCommentRewriter : CSharpSyntaxRewriter
    {
        public override SyntaxNode? VisitPropertyDeclaration(PropertyDeclarationSyntax node)
        {
            // 获取属性名称
            var propertyName = node.Identifier.ValueText;

            // 创建注释节点
            var comment = SyntaxFactory.Trivia(SyntaxFactory.DocumentationCommentTrivia(
                SyntaxKind.MultiLineDocumentationCommentTrivia,
                SyntaxFactory.List<XmlNodeSyntax>(new XmlNodeSyntax[]{
                SyntaxFactory.XmlNewLine("/// <summary>\n"),
                SyntaxFactory.XmlNewLine($"{propertyName} 测试\n"),
                SyntaxFactory.XmlText(SyntaxFactory.XmlTextNewLine("</summary>", false)),
                })));

            // 更新属性节点，将注释节点添加到属性声明前面
            var updatedNode = node.WithLeadingTrivia(comment).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
            return base.VisitPropertyDeclaration(updatedNode);
        }
    }
}
